<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../../lib/innerIframe-include-ext.js"></script>
<script type="text/javascript" src="../../lib/loader-theme.js"></script>
<script type="text/javascript" src="../../lib/ext/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../lib/ext/plugins/DataTip.js"></script>
<script type="text/javascript" src="../../js/moment.min.js"></script>
<script type="text/javascript" src="../../lib/dict.js"></script>
<title>用户积分变动流水</title>
</head>
<script type="text/javascript">
Ext.require(['*']);
Ext.onReady(function(){
    Ext.QuickTips.init();
    Ext.define('model', {
        extend: 'Ext.data.Model',
        fields: [
            'id', 'uid', 'vendor', 'app_id', 'rule_id', 'rule_name', 'rule_origin_id', 'event_id', 'point_type', 
            'amount', 'used_amount', 'pre_amount', 'post_amount', 'join_id', 'status', 
            'begin_date', 'end_date', 'meta', 'create_at', 'update_at'
        ],
        idProperty: 'id'
    });
    
    var store = Ext.create('Ext.data.Store', {
        pageSize: 20,
        model: 'model',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/point/list',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: 'update_at',
                direction: 'ASC'
            }
        ]
    });

    var appStore = Ext.create('Ext.data.JsonStore',{
        fields : ['id','app_id','app_name'],
        autoLoad:true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/apps/combo_store',
            reader: {
                type: 'json'
            },
            simpleSortMode: true,
            listeners:{
                'exception':function( theproxy, response, operation, options ){
                    var data = eval("("+response.responseText+")");
                    if(!data.success){
                        Ext.Msg.alert('信息提示',data.msg);
                    } else {
                        Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                    }
                }
            }
        }
    });

    var ruleStore = Ext.create('Ext.data.JsonStore', {
        fields: ['id', 'rule_name', 'rule_id'],
        autoLoad:true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/rule/combo_list',
            reader: {
                type: 'json'
            },
            simpleSortMode: true,
            listeners:{
                'exception':function( theproxy, response, operation, options ){
                    var data = eval("("+response.responseText+")");
                    if(!data.success){
                        Ext.Msg.alert('信息提示',data.msg);
                    } else {
                        Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                    }
                }
            }
        }
    })
    
    var apps = getApps();

    var grid = Ext.create('Ext.grid.Panel', {
        id:'my_grid',
        store: store,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {
                text: "ID",
                dataIndex: 'id',
                hidden: true
            },{
                text: '规则ID',
                dataIndex:'rule_id',
                hidden: true
            },{
                text: '事件ID',
                dataIndex:'event_id',
                hidden: true
            },{
                text:'用户id',
                dataIndex:'uid',
                width: 65
            },{
                text: '规则名称',
                dataIndex:'rule_name'
            },{
                text:'积分类型',
                dataIndex:'point_type',
                renderer:function(val) {
                    if(val == '1') {
                        return '积分产生';
                    } else if (val == '2') {
                        return '积分消费';
                    } else if (val == '3') {
                        return '过期积分清除';
                    } else {
                        return ;
                    }
                    // if(val == '1')
                    //     return '积分产生';
                    // else if (val == '2')
                    //     return '积分消耗';
                    // else if (val == '3')
                    //     return '积分惩罚';
                    // else
                    //     return '过期积分清除';
                }
            },{
                text:'积分数量',
                dataIndex:'amount'
            },{
                text:'已用数量',
                dataIndex:'used_amount'
            },{
                text:'发生前积分数量',
                dataIndex:'pre_amount',
                width: 115
            },{
                text:'发生后积分数量',
                dataIndex:'post_amount',
                width: 115
            },{
                text:'join_id',
                dataIndex: 'join_id'
            },{
                text:'积分状态',
                dataIndex:'status'
            },
            {
                text:'创建时间',
                dataIndex:'create_at',
                width: 145,
                renderer: function(val) {
                    return moment(val).format('YYYY-MM-DD HH:mm:ss');
                }
            }
            // {
            //     text:'规则开始日期',
            //     dataIndex:'begin_date',
            //     renderer: function(val) {
            //         return moment(val).format('YYYY-MM-DD');
            //     }
            // },{
            //     text:'规则结束日期',
            //     dataIndex:'end_date',
            //     renderer: function(val) {
            //         return moment(val).format('YYYY-MM-DD');
            //     }
            // },{
            //     text:'积分结束日期',
            //     dataIndex:'expiry_date',
            //     renderer: function(val) {
            //         if(val)
            //             return moment(val).format('YYYY-MM-DD');
            //     }
            // }
        ],
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: '当前显示  {0} - {1} 共  {2} 条',
            emptyMsg: "没有数据显示！",
            beforePageText: "页码",
            afterPageText: "共 {0} 页",
            firstText:"首页",
            lastText:"末页",
            nextText:"下一页",
            prevText:"上一页",
            refreshText:"刷新"
        }),
        tbar: [
            ,{
                text: '查看详细信息',
                icon : '../../img/list-items.gif',
                handler: doGetInfo
            },
            {
                xtype:'combo',
                fieldLabel:'规则',
                labelAlign:'right',
                id:'search_rule_name',
                queryParam: 'rule_name',
                minChars: 2,
                editable:true,
                store: ruleStore,
                queryMode: 'remote',
                labelWidth:45,
                width:200,
                displayField: 'rule_name',
                valueField: 'id'
            },{
                xtype:'combo',
                fieldLabel:'产品线',
                labelAlign:'right',
                id:'search_app_name',
                queryParam: 'app_name',
                minChars: 2,
                editable:true,
                store: appStore,
                queryMode: 'remote',
                labelWidth:60,
                width:200,
                displayField: 'app_name',
                valueField: 'app_id'
            },{
                text: '搜  索',
                icon : '../../img/search.png',
                handler: doSearch
            }],
            listeners: {
                'itemdblclick': function(view, record, item, index, e, eOpts) {
                    doGetInfo();
                }
            }
    });
    store.loadPage(1);
    
    function doSearch() {
        var params  = {};
        params.origin_id = Ext.getCmp('search_rule_name').getValue();
        params.app_id = Ext.getCmp('search_app_name').getValue();
        var proxy = store.getProxy();
        proxy.extraParams = params;
        store.loadPage(1);
    }

    var viewport = Ext.create('Ext.Viewport', {
        id: 'border-example',
        layout: 'fit',
        items: grid
    });
    var addWin;
    var form;
    form = new Ext.FormPanel({
        id: 'myForm',
        method:'POST',
        frame:true,
        style:'border-width:0 0 0 0;',
        bodyBorder:false,
        bodyPadding: '10 40 0 0',
        layout: 'form',
        autoScroll: true,
        plugins: {
            ptype: 'datatip'
        },
        defaults:{
            labelAlign:'right',
            readOnly:true
            //labelWidth:120
        },
        items: [
            {
                xtype:'hidden',
                name:'id'
            },{
                xtype:'hidden',
                name:'origin_id'
            },{
                xtype:'hidden',
                name:'create_at'
            },{
                xtype:'hidden',
                name:'priority'
            },{
                xtype:'hidden',
                name:'params'
            },{
                xtype:'hidden',
                name:'status'
            },{
                xtype:'textfield',
                fieldLabel:'操作时间',
                name:'update_at',
                labelAlign: 'right'
            },{
                xtype:'textarea',
                fieldLabel:'操作日志',
                name:'command',
                id:'command',
                labelAlign: 'right'
            },{
                xtype:'textfield',
                fieldLabel:'规则名称',
                name:'rule_name',
                labelAlign: 'right'
            },{
                xtype:'textfield',
                fieldLabel:'规则ID/编码',
                name:'rule_id',
                labelAlign: 'right'
            },{
                xtype:'combo',
                fieldLabel:'应用选择',
                allowBlank:false,
                name:'app_id',
                editable:false,
                //regex : /^[0-9]*$/,
                //regexText:'只能是数字',
                store: appStore,
                queryMode: 'remote',
                width:80,
                displayField: 'app_name',
                valueField: 'app_id'
            },{
                xtype:'combo',
                fieldLabel:'发行/成本方',
                editable:false,
                allowBlank:false,
                name:'vendor',
                //regex : /^[0-9]*$/,
                //regexText:'只能是数字',
                store: Ext.create('Ext.data.Store', {
                    fields: ['vendor_name', 'vendor_id'],
                    data : [{'vendor_name':'财富部','vendor_id':'cfb'},{'vendor_name':'资管部','vendor_id':'zgb'}]
                }),
                queryMode: 'local',
                width:80,
                displayField: 'vendor_name',
                valueField: 'vendor_id'
            },{
                xtype: 'container',
                layout:{
                    type:'hbox'
                },
                defaults:{
                    labelAlign: 'right'
                },
                style: {marginTop: '3px',marginLeft: '15px'},
                items:[
                    {
                        xtype : 'datefield',
                        name : 'begin_date',
                        fieldLabel : '规则开始日期',
                        labelWidth : 85,
                        editable:false,
                        width:195,
                        format : 'Y-m-d'
                    },{
                        xtype : 'datefield',
                        name : 'end_date',
                        fieldLabel : '规则结束日期',
                        style: {marginLeft: '25px'},
                        labelWidth : 85,
                        editable:false,
                        width:195,
                        format : 'Y-m-d'
                    }
                ]
            },{
                xtype: 'container',
                layout:{
                    type:'hbox'
                },
                defaults:{
                    labelAlign: 'right'
                },
                style: {marginTop: '3px',marginLeft: '15px'},
                items:[
                    {
                        xtype : 'datefield',
                        name: 'expiry_date',
                        fieldLabel : '积分结束日期',
                        labelWidth : 85,
                        width:195,
                        format : 'Y-m-d'
                    },{
                        xtype : 'numberfield',
                        name:'period_day',
                        fieldLabel : '积分有效天数',
                        style: {marginLeft: '25px'},
                        labelWidth : 85,
                        width:195
                    }
                ]
            },{
                xtype: 'container',
                layout:{
                    type:'hbox'
                },
                defaults:{
                    labelAlign: 'right'
                },
                style: {marginTop: '3px',marginLeft: '15px'},
                items:[
                    {
                        xtype : 'numberfield',
                        name:'amount',
                        fieldLabel : '设置积分数量',
                        labelWidth : 85,
                        allowBlank: false,
                        width:195,
                    },{
                        xtype : 'numberfield',
                        name:'frequency',
                        fieldLabel : '设置积分频率',
                        style: {marginLeft: '25px'},
                        labelWidth : 85,
                        allowBlank: true,
                        width:195
                    }
                ]
            },{
                xtype: 'radiogroup',
                fieldLabel: '频率单位',
                vertical: true,
                id:'frequency_unit',
                items: [
                    {
                        boxLabel: '天',
                        name: 'frequency_unit',
                        id:'frequency_unit_d',
                        inputValue: 'd'
                    }, {
                        boxLabel: '周',
                        name: 'frequency_unit',
                        id:'frequency_unit_w',
                        inputValue: 'w'
                    }, {
                        boxLabel: '月',
                        name: 'frequency_unit',
                        id:'frequency_unit_m',
                        inputValue: 'm'
                    }, {
                        boxLabel: '年',
                        name: 'frequency_unit',
                        id:'frequency_unit_y',
                        inputValue: 'y'
                    }
                ]
            },{
                xtype: 'radiogroup',
                fieldLabel: '积分类型',
                vertical: true,
                id:'rule_type',
                items: [
                    {
                        boxLabel: '积分产生',
                        name: 'rule_type',
                        id: 'rule_type_1',
                        inputValue: '1'
                    }, {
                        boxLabel: '积分消费',
                        name: 'rule_type',
                        id: 'rule_type_2',
                        inputValue: '2'
                    }, {
                        boxLabel: '积分惩罚',
                        name: 'rule_type',
                        id: 'rule_type_3',
                        inputValue: '3'
                    }, {
                        boxLabel: '过期积分清除',
                        name: 'rule_type',
                        id: 'rule_type_4',
                        inputValue: '4'
                    }
                ]
            },
            // {
            //     xtype: 'radiogroup',
            //     fieldLabel: '规则优先级',
            //     vertical: true,
            //     id:'priority',
            //     items: [
            //         {
            //             boxLabel: '从小到大',
            //             name: 'priority',
            //             inputValue: '1'
            //         }, {
            //             boxLabel: '从高到低',
            //             name: 'priority',
            //             inputValue: '2'
            //         }
            //     ]
            // },
            {
                xtype: 'radiogroup',
                fieldLabel: '规则进入条件类型',
                vertical: true,
                id:'cond_type',
                items: [
                    {
                        boxLabel: '简单规则',
                        name: 'cond_type',
                        id: 'cond_type_0',
                        inputValue: '0'
                    }, {
                        boxLabel: '复合规则',
                        name: 'cond_type',
                        id: 'cond_type_1',
                        inputValue: '1'
                    }
                ]
            },{
                xtype:'textfield',
                fieldLabel:'用户提示',
                name:'prompt',
                labelAlign: 'right'
            },{
                xtype:'textarea',
                fieldLabel:'规则说明',
                name:'remark',
                labelAlign: 'right'
            },{
                xtype:'textarea',
                fieldLabel:'复合规则执行的内容',
                name:'condition',
                labelAlign: 'right'
            }
        ],
        buttons:[{
            text:'关  闭',
            icon:'../../img/close.png',
            handler:function(){
                this.up('form').getForm().reset();
                addWin.close();
            }
        }]
    });
    function doGetInfo() {
        var records = grid.getSelectionModel().getSelection();
        if(records.length == 0){
            Ext.Msg.alert("信息提示", "请选择一条规则历史记录！");
            return;
        }
        if(records.length > 1){
            Ext.Msg.alert('信息提示', '只能选择一条记录查看！');
            return;
        }
        form.form.reset();
        if(!addWin){
            addWin = Ext.create('Ext.Window', {
                title: '查看规则详细信息',
                width: 666,
                height: 620,
                modal: true,
                constrainHeader: true,
                closeAction:'hide',
                layout: 'fit',
                items: form
            });
        }
        Ext.Ajax.request({
            url: '/rule/jour_detail',
            params : {'id' : records[0].get('id')},
            method : 'get',
            success: function(response, opts) {
                var jsonResult = Ext.decode(response.responseText); 
                if(jsonResult.success == false){
                    return alert(jsonResult.error || jsonResult.msg);
                }
                if(!jsonResult){
                    return ;
                }
                for(var i in jsonResult){
                    if(form.form.findField(i)){
                        form.form.findField(i).setValue(jsonResult[i]);
                    }
                }
                //设置时间
                if(jsonResult.update_at){
                    form.form.findField('update_at').setValue(moment(jsonResult.update_at).format('YYYY-MM-DD HH:mm:ss'));
                }
                if(jsonResult.begin_date){
                    form.form.findField('begin_date').setValue(moment(jsonResult.begin_date).format('YYYY-MM-DD'));
                }
                if(jsonResult.end_date){
                    form.form.findField('end_date').setValue(moment(jsonResult.end_date).format('YYYY-MM-DD'));
                }
                if(jsonResult.expiry_date) {
                    form.form.findField('expiry_date').setValue(moment(jsonResult.expiry_date).format('YYYY-MM-DD'));
                }
                //设置单选框
                if(jsonResult.frequency_unit) {
                    Ext.getCmp('frequency_unit').getComponent('frequency_unit_'+jsonResult.frequency_unit).setValue(true);
                }
                if(jsonResult.rule_type) {
                    Ext.getCmp('rule_type').getComponent('rule_type_'+jsonResult.rule_type).setValue(true);
                }
                if(jsonResult.cond_type) {
                    Ext.getCmp('cond_type').getComponent('cond_type_'+jsonResult.cond_type).setValue(true);
                }
                addWin.show();
            }
        });
    }

    function delHandler(status){
        var records = grid.getSelectionModel().getSelection();
        var msg1, msg2;
        if(status == '0') {
            msg1 = "请选择一条需要立即生效的规则！";
            msg2 = "确认立即生效吗？";
        } else {
            msg1 = "请选择一条需要立即失效的规则";
            msg2 = "确认立即失效吗？";
        }
        if(records.length == 0){
            Ext.Msg.alert("信息提示", msg1);
            return;
        }
        if(records.length > 1){
            Ext.Msg.alert('信息提示', '不允许批量操作！');
            return;
        }
        var status_now = records[0].get('status');
        if(status == status_now) {
            Ext.Msg.alert('信息提示', '该规则当前状态不支持此操作！');
            return;
        }
        Ext.MessageBox.confirm('信息提示',msg2,function(val){
            if(val == 'yes'){
                Ext.Ajax.request({
                    url: '/rule/del?id='+records[0].get("id")+'&status='+status,
                    method : 'POST',
                    success: function(response, opts) {
                        var data = eval("("+response.responseText+")");
                        Ext.Msg.alert('信息提示',data.msg);
                        store.reload();
                    },
                    failure: function(response, opts) {
                        var data = eval("("+response.responseText+")");
                        if(!data.success){
                            Ext.Msg.alert('信息提示',data.msg || data.error);
                        } else {
                            Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                        }
                    }
                });
            }
        });
    }
});
</script>
<body>
</body>
</html>